package hk.m4s.lr.repair.test.ui.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import framework.common.Constant;
import framework.common.baseui.UeBaseActivity;
import framework.utils.SharedPreferencesUtils;
import hk.m4s.lr.repair.test.R;
import hk.m4s.lr.repair.test.model.OrderDetailModel;
import hk.m4s.lr.repair.test.ui.adapter.LookPayOderAdapter;
import hk.m4s.lr.repair.test.ui.goods.PayMoneyActivity;
import hk.m4s.lr.repair.test.views.TipsDialog;
import hk.m4s.lr.repair.test.views.listview.InnerListView;
import okhttp3.Response;

/**
 * Created by qi.wang on 2017/5/18.
 * 查看订单
 */

public class LookUnprocessedOrderActivity extends UeBaseActivity {
    private LinearLayout sendTimeLayout;
    private LinearLayout payTimeLayout,topTips,orderNumLayout;
    private RelativeLayout dixianLayout, quandixianLayout, scLayout;
    private TextView user_name, user_phone, user_address, orderNum, orderTime, pay_order_score, order_shop_name, order_shop_price, order_shop_num;
    private TextView pay_order_yun, quandixian_money, dixian_money, jiangli_sc, order_pay_time;
    private TextView order_num,order_create_time,pay_order_name,pay_order_num,pay_time,cancelOrder,order_send_time,tipText;
    private ImageView order_shop_img;
    private String state;
    private InnerListView lookListView;
    private LookPayOderAdapter lookOderAdapter;
    private List<OrderDetailModel.GoodsInfoBean> modelList = new ArrayList<>();
    String orderId;
    private Context mContext;
    String totalMoney = "0.0";
    String yunMoney = "0.0";
    String actual_money = "0.0";
    public static LookUnprocessedOrderActivity instance;
    String oilMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseSetContentView(R.layout.activity_pay_moneyorder_look);
        hiddenFooter();
        showGoBackBtn();
        setTitleText("订单详情");
        mContext = this;
        orderId = getIntent().getStringExtra("orderId");
        instance = this;
        findBaseView();
    }

    public void findBaseView() {
        order_shop_img = (ImageView) findViewById(R.id.order_shop_img);
        tipText = (TextView) findViewById(R.id.tipText);
        order_shop_name = (TextView) findViewById(R.id.order_shop_name);
        order_shop_price = (TextView) findViewById(R.id.order_shop_price);
        order_shop_num = (TextView) findViewById(R.id.order_shop_num);
        cancelOrder= (TextView) findViewById(R.id.cancelOrder);
        order_send_time= (TextView) findViewById(R.id.order_send_time);
        orderNum = (TextView) findViewById(R.id.orderNum);
        orderTime = (TextView) findViewById(R.id.orderTime);
        user_name = (TextView) findViewById(R.id.user_name);
        user_phone = (TextView) findViewById(R.id.user_phone);
        user_address = (TextView) findViewById(R.id.user_address);
        pay_order_score = (TextView) findViewById(R.id.pay_order_score);
        pay_order_yun = (TextView) findViewById(R.id.pay_order_yun);
        quandixian_money = (TextView) findViewById(R.id.quandixian_money);
        dixian_money = (TextView) findViewById(R.id.dixian_money);
        jiangli_sc = (TextView) findViewById(R.id.jiangli_sc);
        order_num= (TextView) findViewById(R.id.order_num);
        order_create_time= (TextView) findViewById(R.id.order_create_time);
        pay_order_name= (TextView) findViewById(R.id.pay_order_name);
        order_pay_time = (TextView) findViewById(R.id.order_pay_time);
        pay_order_num= (TextView) findViewById(R.id.pay_order_num);
        pay_time= (TextView) findViewById(R.id.pay_time);
        orderNumLayout= (LinearLayout) findViewById(R.id.orderNumLayout);
        sendTimeLayout = (LinearLayout) findViewById(R.id.sendTimeLayout);
        topTips= (LinearLayout) findViewById(R.id.topTips);
        payTimeLayout = (LinearLayout) findViewById(R.id.payTimeLayout);


        lookListView = (InnerListView) findViewById(R.id.lookListView);
        quandixianLayout = (RelativeLayout) findViewById(R.id.quandixianLayout);
        dixianLayout = (RelativeLayout) findViewById(R.id.dixianLayout);
        scLayout = (RelativeLayout) findViewById(R.id.scLayout);
        oilMoney=getIntent().getStringExtra("oilMoney");
        state=getIntent().getStringExtra("statuts");
        if(state!=null){
            if(state.equals("1")){
                orderNumLayout.setVisibility(View.GONE);
                topTips.setVisibility(View.VISIBLE);
                payTimeLayout.setVisibility(View.VISIBLE);
                tipText.setText("等待付款");
                Drawable drawable = getResources().getDrawable(R.mipmap.waitmoneyimg);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                tipText.setCompoundDrawables(drawable, null, null, null);
                cancelOrder.setText("取消订单");
                order_send_time.setText("立即支付");
            }else
            if(state.equals("2")){
                payTimeLayout.setVisibility(View.GONE);
                orderNumLayout.setVisibility(View.VISIBLE);
                topTips.setVisibility(View.GONE);
                cancelOrder.setText("申请售后");
                order_send_time.setText("催发货");
            }else if (state.equals("3")){
                payTimeLayout.setVisibility(View.GONE);
                orderNumLayout.setVisibility(View.VISIBLE);
                topTips.setVisibility(View.VISIBLE);
                tipText.setText("等待收货");
                Drawable drawable = getResources().getDrawable(R.mipmap.waitegoodsimg);
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                tipText.setCompoundDrawables(drawable, null, null, null);
                cancelOrder.setText("查看物流");
                order_send_time.setText("确认收货");
            }
        }else{
            topTips.setVisibility(View.GONE);
            orderNumLayout.setVisibility(View.GONE);
        }
       // findOrderDetails();
        lookOderAdapter = new LookPayOderAdapter(mContext, modelList);
        lookListView.setAdapter(lookOderAdapter);
    }

//    public void findOrderDetails() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("orderId", orderId);
//        MyService.findOrderDetails(mContext, parmas, new ResponseCallback<OrderDetailModel>() {
//            @Override
//            public void onSuccess(OrderDetailModel orderDetailModel) {
//                modelList.clear();
//                if (orderDetailModel.getAddressInfo() != null) {
//                    user_name.setText("收货人:  " + orderDetailModel.getAddressInfo().getName());
//                    user_phone.setText(orderDetailModel.getAddressInfo().getPhone());
//                    String address = "";
//                    if (orderDetailModel.getAddressInfo().getProvince() != null) {
//                        address = orderDetailModel.getAddressInfo().getProvince();
//                    }
//                    if (orderDetailModel.getAddressInfo().getCity() != null) {
//                        address = address + orderDetailModel.getAddressInfo().getCity();
//                    }
//                    if (orderDetailModel.getAddressInfo().getArea() != null) {
//                        address = address + orderDetailModel.getAddressInfo().getArea();
//                    }
//                    address = address + orderDetailModel.getAddressInfo().getAddress();
//                    user_address.setText(address);
//                }
//                if (orderDetailModel.getOrderInfo() != null) {
//                    orderNum.setText(AppTools.getStrTimes(orderDetailModel.getOrderInfo().getAdd_time()));
//                    String endTime = orderDetailModel.getOrderInfo().getClose_time();
//                    orderTime.setText(AppTools.secondToTime(Long.parseLong(endTime)));
//                    order_create_time.setText(AppTools.getStrTimes(orderDetailModel.getOrderInfo().getAdd_time()));
//                    if (!(orderDetailModel.getOrderInfo().getOrder_money() == null || orderDetailModel.getOrderInfo().getOrder_money().equals(""))) {
//                        pay_order_score.setText(MoneyTool.getLocalMoney(orderDetailModel.getOrderInfo().getOrder_money()));
//                        actual_money= orderDetailModel.getOrderInfo().getOrder_money();
//
//                    }
//                    if (!(orderDetailModel.getOrderInfo().getActual_money() == null || orderDetailModel.getOrderInfo().getActual_money().equals(""))) {
//                        pay_order_num.setText(MoneyTool.getLocalMoney(orderDetailModel.getOrderInfo().getActual_money()));
//                    }
//
//                    if (!(orderDetailModel.getOrderInfo().getFreight() == null || orderDetailModel.getOrderInfo().getFreight().equals(""))) {
//                        pay_order_yun.setText("+" + MoneyTool.getLocalMoney(orderDetailModel.getOrderInfo().getFreight()));
//                        yunMoney = orderDetailModel.getOrderInfo().getFreight();
//                    }
//                    if (!(orderDetailModel.getOrderInfo().getCoupon_arrive() == null || orderDetailModel.getOrderInfo().getCoupon_arrive().equals(""))) {
//                        quandixianLayout.setVisibility(View.VISIBLE);
//                        quandixian_money.setText(MoneyTool.getLocalMoney(orderDetailModel.getOrderInfo().getCoupon_arrive()));
//                    } else {
//                        quandixianLayout.setVisibility(View.GONE);
//                    }
//                    if (!(orderDetailModel.getOrderInfo().getAward_arrive() == null || orderDetailModel.getOrderInfo().getAward_arrive().equals(""))) {
//                        dixianLayout.setVisibility(View.VISIBLE);
//                        dixian_money.setText(MoneyTool.getLocalMoney(orderDetailModel.getOrderInfo().getAward_arrive()));
//                    } else {
//                        dixianLayout.setVisibility(View.GONE);
//                    }
//                    if(orderDetailModel.getOrderInfo().getPayment()!=null) {
//                        if (orderDetailModel.getOrderInfo().getPayment().equals("2")) {
//                            pay_order_name.setText("支付宝支付");
//                        } else if (orderDetailModel.getOrderInfo().getPayment().equals("1")) {
//                            pay_order_name.setText("微信支付");
//                        }else{
//                            pay_order_name.setText("--");
//                        }
//                    }
//                    if (!(orderDetailModel.getOrderInfo().getPayment_time() == null || orderDetailModel.getOrderInfo().getPayment_time().equals(""))) {
//                        pay_time.setText(AppTools.getStrTimes(orderDetailModel.getOrderInfo().getPayment_time()));
//                    }
//                    if(!(orderDetailModel.getOrderInfo().getOrder_number()==null)){
//                        order_num.setText(orderDetailModel.getOrderInfo().getOrder_number());
//                    }
//                    if (!(orderDetailModel.getOilRatio() == null || orderDetailModel.getOilRatio().equals(""))) {
//                        scLayout.setVisibility(View.GONE);
//                        oildLayout.setVisibility(View.VISIBLE);
//                        order_shop_name.setText(orderDetailModel.getOilRatio().getCompany());
//                        order_shop_price.setText(MoneyTool.getLocalMoney(orderDetailModel.getOilRatio().getNominal_value()));
//                        order_shop_num.setText("x" + orderDetailModel.getOilRatio().getCount());
//                        try {
//                            Glide.with(mContext).load(orderDetailModel.getOilRatio().getImg()).asBitmap().placeholder(R.mipmap.upload_photo).error(R.mipmap.upload_photo).into(order_shop_img);
//                        } catch (Exception e) {
//                            e.printStackTrace();
//                        }
//                    } else {
//                        oildLayout.setVisibility(View.GONE);
//                        scLayout.setVisibility(View.GONE);
//                    }
//                    if (!(orderDetailModel.getOrderInfo().getActual_money() == null || orderDetailModel.getOrderInfo().getActual_money().equals(""))) {
//                        order_pay_time.setText(MoneyTool.getLocalMoney(orderDetailModel.getOrderInfo().getActual_money()));
//                        totalMoney  = orderDetailModel.getOrderInfo().getActual_money();
//                    }
//                    if (orderDetailModel.getGoodsInfo() != null) {
//                        modelList.addAll(orderDetailModel.getGoodsInfo());
//                        lookOderAdapter.notifyDataSetChanged();
//                    }
//                }
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.order_send_time:
                if(state.equals("1")) {
                    Intent intent = new Intent(LookUnprocessedOrderActivity.this, PayMoneyActivity.class);
//                    intent.putExtra("totalMoney", totalMoney);
//                    intent.putExtra("yunMoney", yunMoney);
//                    intent.putExtra("actual_money", actual_money);
//                    intent.putExtra("orderId", orderId);
//                    if (oilMoney != null) {
//                        intent.putExtra("show", "2");
//                        intent.putExtra("oilMoney", oilMoney);
//                    } else {
//                        intent.putExtra("show", "1");
//                    }
                    intent.putExtra("goPay", "2");
                    startActivity(intent);
                }else if(state.equals("2")){
                    //pushcargo(orderId);
                    TipsDialog dialog=new TipsDialog(mContext);
                    dialog.show();
                }else if(state.equals("3")){
                    //updateOrderStatus();
                }
                break;
            case R.id.cancelOrder:
                Intent intents=null;
                if(state.equals("1")) {
                   intents = new Intent(LookUnprocessedOrderActivity.this, CancalOrderActivity.class);
                    intents.putExtra("type", "1");
                }else if(state.equals("2")){
                    intents = new Intent(LookUnprocessedOrderActivity.this, TuiMoneyActivity.class);
                    intents.putExtra("type", state);
                }else
                if(state.equals("3")) {
                   // intents.putExtra("model", (Serializable) modelList);
                   // intents.putExtra("orderId", orderId);
                   intents.putExtra("type", "2");
                   // startActivity(intents);
                  intents = new Intent(LookUnprocessedOrderActivity.this, OrderSmSActivity.class);
                    //intent.putExtra("orderId", detailBean.getOrder_id());
                    intents.putExtra("type", "0");

                }
                startActivity(intents);
                break;
        }
    }
//    public void updateOrderStatus() {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("orderId",orderId);
//        parmas.put("status","6");
//        AccountSerive.updateOrderStatus(mContext, parmas, new ResponseCallback<JiangliModel>() {
//            @Override
//            public void onSuccess(JiangliModel model) {
//                finish();
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }
//    public void pushcargo(String orderId) {
//        Map<String, Object> parmas = new HashMap<>();
//        parmas.put("device_type", Constant.device_type);
//        parmas.put("token", SharedPreferencesUtils.getSharedPreferences(Constant.token, ""));
//        parmas.put("userId", SharedPreferencesUtils.getSharedPreferences(Constant.userId, ""));
//        parmas.put("orderid", orderId);
//        AccountSerive.pushcargo(mContext, parmas, new ResponseCallback<GoodsCarModel>() {
//            @Override
//            public void onSuccess(GoodsCarModel model) {
//                ToastUtil.toast(mContext,"提醒商家成功");
//            }
//
//            @Override
//            public void onError(Response response, int errorCode, String errorMsg, Exception e) {
//
//            }
//        });
//
//    }
}
